<template>
  <div>
    <el-drawer
      v-if="drawer"
      ref="selectDialog"
      :size="width"
      class="select-drawer"
      :show-close="true"
      :title="title"
      :visible.sync="visible"
      direction="rtl"
      :wrapper-closable="false"
      :close-on-press-escape="false"
      :destroy-on-close="false"
      v-bind="$attrs"
    >
      <slot />
      <div class="drawer-footer">
        <el-button size="mini" @click="cancel">
          {{ cancelText }}
        </el-button>
        <el-button type="primary" size="mini" @click="ok">
          {{ confirmText }}
        </el-button>
      </div>
    </el-drawer>
    <el-dialog
      v-else
      ref="selectDialog"
      :custom-class="customClass"
      class="select-dialog"
      :title="title"
      :visible.sync="visible"
      :show-close="true"
      :append-to-body="appendToBody"
      :width="width"
      top="10vh"
      :before-close="cancel"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="false"
      v-bind="$attrs"
      @close="onClose"
    >
      <slot />
      <div slot="footer">
        <el-button size="mini" @click="cancel">
          {{ cancelText }}
        </el-button>
        <el-button type="primary" size="mini" @click="ok">
          {{ confirmText }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'SelectDialog',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    customClass: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '90%'
    },
    appendToBody: {
      type: Boolean,
      default: true
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    confirmText: {
      type: String,
      default: '确定'
    },
    drawer: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
    }
  },
  computed: {
    visible: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  methods: {
    onClose() {
      const c = this.$refs.selectDialog.$children.find(it => it.$el.classList.contains('table-app-container'))
      if (c) {
        c.clearSelection()
      }
    },
    ok() {
      this.$emit('on-ok')
    },
    cancel() {
      this.$emit('on-cancel')
    }
  }
}
</script>
